package com.github.live.component;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.github.live.R;
import com.github.live.base.KeyEventDispatcher;
import com.github.live.manager.ChannelsManager;
import com.github.live.manager.LiveInitializer;
import com.github.livebase.bean.Channel;
import com.github.livebase.util.base.NumberUtil;
import com.github.livebase.util.base.ObjectsUtil;

import java.lang.ref.WeakReference;

/**
 * @ClassName ChannelSelector
 * @description:
 * @author: biao532
 * @create: 2023-12-24 11:04
 * @Version 1.0
 **/
@SuppressLint("AppCompatCustomView")
public class ChannelSelectorView extends LinearLayout implements KeyEventDispatcher {
    private WeakReference<ChannelSelectorView> _this = new WeakReference<>(this);
    private Runnable runnable = () -> {
        ChannelSelectorView __this = _this.get();
        if (__this == null) {
            return;
        }
        __this.setVisibility(INVISIBLE);
        if (__this.data.isEmpty()) {
            return;
        }
        __this.jumpTarget();
    };
    private String data = ObjectsUtil.Empty.STRING;
    private TextView number_box;
    private TextView name_box;
    private Handler mHandler = new Handler();

    public ChannelSelectorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.component_channel_selector, this);
        number_box = findViewById(R.id.selector_number);
        name_box = findViewById(R.id.selector_name);
    }

    @Override
    public boolean dispatch(int keyCode, KeyEvent keyEvent) {
        if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) {
            if (this.getVisibility() != VISIBLE) {
                this.data = ObjectsUtil.Empty.STRING;
                setVisibility(VISIBLE);
            }
            data += keyCode - KeyEvent.KEYCODE_0;
            // Integer.MAX_VALUE = 2147483647
            if (data.length() > 9) {
                data = data.substring(data.length() - 9);
            }
            setChannelName(data);
            number_box.setText(data);
            resetRunning();
            return true;
        }
        switch (keyCode) {
            case KeyEvent.KEYCODE_ESCAPE:
            case KeyEvent.KEYCODE_BACK: {
                if (this.getVisibility() != VISIBLE) {
                    break;
                }
                if (data.length() > 0) {
                    this.data = data.substring(0, data.length() - 1);
                }
                if (data.length() == 0) {
                    mHandler.removeCallbacks(runnable);
                    setVisibility(INVISIBLE);
                }
                setChannelName(data);
                number_box.setText(data);
                resetRunning();
                return true;
            }
            case KeyEvent.KEYCODE_DPAD_CENTER:
            case KeyEvent.KEYCODE_ENTER:
            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE: {
                if (this.getVisibility() != VISIBLE) {
                    break;
                }
                jumpTarget();
                mHandler.removeCallbacks(runnable);
                this.setVisibility(INVISIBLE);
                return true;
            }
        }
        if (getVisibility() == VISIBLE) {
            mHandler.removeCallbacks(runnable);
            this.setVisibility(INVISIBLE);
        }
        return KeyEventDispatcher.super.dispatch(keyCode, keyEvent);
    }
    private void setChannelName(String data) {
        int number = NumberUtil.toInteger(data, -1);
        if (number == -1) {
            return;
        }
        Channel channel = ChannelsManager.getChannelByFilter(
                (c)-> c.getNumber() == number
        );
        if (channel != null) {
            name_box.setText(channel.getName());
        }
        else {
            name_box.setText("频道不存在");
        }
    }

    private void resetRunning() {
        mHandler.removeCallbacks(runnable);
        mHandler.postDelayed(runnable, LiveInitializer.getInitializer()
                .getConfig()
                .getSelectWaitfor());
    }

    private void jumpTarget() {
        LiveInitializer.getInitializer().
                getChannelsManager().
                setActiveByNumber(Integer.valueOf(this.data));
        this.data = ObjectsUtil.Empty.STRING;
    }
}
